package io.geekidea.boot.inventorychange.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.protobuf.Api;
import io.geekidea.boot.auth.util.LoginUtil;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.inventorychange.dto.StoreChangeDto;
import io.geekidea.boot.inventorychange.dto.StoreChangeExcel;
import io.geekidea.boot.inventorychange.entity.StoreChange;
import io.geekidea.boot.inventorychange.query.StoreChangeQuery;
import io.geekidea.boot.inventorychange.service.StoreChangeService;
import io.geekidea.boot.inventorychange.vo.StoreChangeVo;
import io.geekidea.boot.persmanag.dto.PersManagExcel;
import io.geekidea.boot.persmanag.query.PersManagQuery;
import io.geekidea.boot.store.entity.Store;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/storeChange")
@Tag(name = "库存变动记录")
public class StoreChangeController {

    @Resource
    private StoreChangeService storeChangeService;


    @PostMapping("/list")
    @Operation(summary = "库存变动记录列表",description = "按多条件查询")
    public ApiResult storeChangeList(@RequestBody StoreChangeQuery storeChangeQuery) throws Exception {
       // StoreChange storeChangeEntity = new StoreChange();
    //    storeChangeEntity.setCreatedBy(LoginUtil.getUsername());
        List<StoreChange> storeChanges = storeChangeService.StoreChanagList(storeChangeQuery);
        int total = storeChangeService.getTotal(storeChangeQuery).getTotal();
        PageDto<StoreChange> pageDto = new PageDto<>(storeChanges,total,storeChangeQuery.getPageSize(),storeChangeQuery.getPageIndex()+1);
        return ApiResult.success(pageDto);
    }


    @PostMapping("/typeList")
    @Operation(summary = "库存变动类型列表")
    public ApiResult stoTypeList(@RequestBody StoreChangeQuery storeChangeQuery){
        List<StoreChangeVo> storeChanges = storeChangeService.typeList(storeChangeQuery);
        return ApiResult.success(storeChanges);
    }


    @Operation(summary = "查询门店id和名称")
    @PostMapping("/stoList")
    public ApiResult storeList(@RequestBody StoreChangeQuery storeChangeQuery){
        List<Store> stores = storeChangeService.StoreList(storeChangeQuery);
        return ApiResult.success(stores);
    }

    //添加库存变更记录
    public ApiResult addStoreChange(@RequestBody StoreChangeDto storeChangeDto) throws Exception {

        storeChangeDto.setCreatedBy(LoginUtil.getUsername());


        boolean b = storeChangeService.addStoreChangeList(storeChangeDto);
        if(!b){
            return ApiResult.success("添加失败");
        }
        return ApiResult.success();
    }


    @PostMapping("/download")
    @Operation(tags = "Post",description = "下载文件")
    public void download(@RequestBody StoreChangeQuery storeChangeQuery, HttpServletResponse response){
        //WarehousentryQuery warehousentryQuery = new WarehousentryQuery();

        List<StoreChangeExcel> list = storeChangeService.getDownloadList(storeChangeQuery);
        OutputStream outputStream = null;
        try {
            response.setCharacterEncoding("UTF-8");
            String excelName = URLEncoder.encode("库存变动记录"+ LocalDateTime.now(), "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + excelName + ".xlsx");
            // 以字节流的形式输出响应正文
            outputStream = response.getOutputStream();
            try (ExcelWriter excelWriter = EasyExcel.write(outputStream, StoreChangeExcel.class).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
                excelWriter.write(list,writeSheet);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}
